﻿
using System;
namespace SuperSocialShare.Helper
{
    internal class ImageTypeHelper
    {
        internal static ImageType GetImageType(byte[] bytes)
        {
            /*
            * 主要图片类型的文件头：
            * JPEG (jpg)：FFD8FF
            * PNG (png)：89504E47
            * GIF (gif)：47494638
            * TIFF (tif)：49492A00
            * Windows Bitmap (bmp)：424D
            */
            if (bytes.Length < 4)
                throw new Exception("The data of image is too short");
            else if (bytes[0] == 0xFF && bytes[1] == 0xD8 && bytes[2] == 0xFF)
                return ImageType.JPEG;
            else if (bytes[0] == 0x47 && bytes[1] == 0x49 && bytes[2] == 0x46 && bytes[3] == 0x38)
                return ImageType.GIF;
            else if (bytes[0] == 0x89 && bytes[1] == 0x50 && bytes[2] == 0x4E && bytes[3] == 0x47)
                return ImageType.PNG;
            else if (bytes[0] == 0x42 && bytes[1] == 0x4D)
                return ImageType.BMP;
            return ImageType.Unkown;
        }

        internal static string GetContentType(ImageType type)
        {
            switch (type)
            {
                case ImageType.BMP:
                    return "image/bmp";
                case ImageType.GIF:
                    return "image/gif";
                case ImageType.JPEG:
                    return "image/jpg";
                case ImageType.PNG:
                    return "image/png";
                default:
                    return "image/unkown";
            }
        }
    }
}
